<template>
	<view style="height: 100%;">
		<cu-custom :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">我的关注</block>
		</cu-custom>
		<scroll-view scroll-x class="bg-white nav">
			<view class="flex text-center">
				<view class="cu-item flex-sub" :class="index==TabCur?'text-orange cur':''" v-for="(item,index) in navItems" :key="index"
				 @tap="tabSelect" :data-id="index">
					{{item}}
				</view>
			</view>
		</scroll-view>
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :up="upOption">
			<view v-for="(item,index) in dataList" :key="index">
				<view v-if="TabCur==0">
					<topic-item @attention="attentionAction(item,index)" @click="clickTopicItem(item)" @clickOwner="jumpToUserProfile(item.owner_id)" @chooseTopicAction="chooseTopicAction(item)" :topic="item"></topic-item>
				</view>
				<view v-if="TabCur==1">
					<list-user-item @click="jumpToUserProfile(item.user_id)" :userInfo="item"></list-user-item>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import ListUserItem from '@/components/list-user-item.vue'
	import TopicItem from '@/components/topic-item.vue'
	import {
		getTimeDistance,
		cutDateFromDateTime
	} from '@/api/utils.js'
	import {
		getTopicList,
		getUserAttentionList,
		getAttentionTopicList,
		attentionTopic
	} from '@/api/api.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import config from '@/api/config.js'
	export default {
		components: {
			TopicItem,
			ListUserItem
		},
		name:"AttentionList",
		mixins: [MescrollMixin],
		data() {
			return {
				themeColor:this.ThemeColor,
				TabCur: 0,
				scrollLeft: 0,
				dataList: [],
				navItems:[
					'话题',
					'达人'
				],
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					textNoMore: config.customNoMore,
					noMoreSize: 1, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
			}
		},
		methods: {
			attentionAction(item,index)
			{
				uni.showLoading({
					title:'正在提交...'
				})
				let status = item.is_attention == 0? 1:0
				attentionTopic(item.topic_id,status).then(res=>{
					uni.hideLoading()
					this.dataList[index].is_attention = status
					let tip = status == 1? '已关注':'已取消'
					uni.showToast({
						title:tip
					})
				}).catch(error=>{
					uni.hideLoading()
				})
			},
			clickTopicItem(item)
			{
				console.log('topic is '+item.title);
				uni.navigateTo({
					url:'/pages/always/topic-post-list/topic-post-list?topicId='+item.topic_id,
					success: function(res) {
						res.eventChannel.emit('topicPostList',item)
					}
				})
			},
			tabSelect(e) {
				this.TabCur = e.currentTarget.dataset.id;
				this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
				if (this.mescroll) {
					this.mescroll.triggerDownScroll()
				}
			},
			jumpToUserProfile(userId) 
			{
				uni.navigateTo({
					url: '/pages/profile/profile?userId=' + userId
				})
			},
			getDataList(pageIndex, pageSize) {
				if(this.TabCur == 0) {
					getAttentionTopicList(pageIndex, pageSize).then(res => {
						res.data.list.forEach(item => {
							item.show_time = getTimeDistance(item.last_message_time)
						})
						let pageList = res.data.list
						if (pageIndex == 0) {
							this.dataList = pageList
						} else {
							this.dataList = this.dataList.concat(pageList)
						}
						let hasNext = pageList.length >= pageSize
						if (this.mescroll) {
							this.mescroll.endSuccess(this.dataList.length, hasNext);
						}
					}).catch(error => {
						if (this.mescroll) {
							this.mescroll.endErr()
						}
					})
				}else{
					getUserAttentionList(pageIndex, pageSize).then(res => {
						res.data.list.forEach(item => {
							item.show_time = cutDateFromDateTime(item.created_at)
						})
						let pageList = res.data.list
						if (pageIndex == 0) {
							this.dataList = pageList
						} else {
							this.dataList = this.dataList.concat(pageList)
						}
						let hasNext = pageList.length >= pageSize
						if (this.mescroll) {
							this.mescroll.endSuccess(this.dataList.length, hasNext);
						}
					}).catch(error => {
						if (this.mescroll) {
							this.mescroll.endErr()
						}
					})
				}
			},
			downCallback() {
				if (this.mescroll) {
					this.mescroll.resetUpScroll()
				}
			},
			upCallback(page) {
				let pageIndex = page.num - 1
				this.getDataList(pageIndex, page.size)
			}
		}
	}
</script>

<style>
</style>
